package com.sgb.member.view_model;

import android.text.TextUtils;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.PriceAdjustmentBean;
import com.sgb.member.ui.fragment.PriceAdjustmentFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class PriceAdjustmentModel extends BaseViewModel {

    private final PriceAdjustmentFragment fragment;
    private final MemberRepertory mRepertory;

    private int current;

    public PriceAdjustmentModel(PriceAdjustmentFragment activity, MemberRepertory repertory) {
        this.fragment = activity;
        this.mRepertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    public void getPriceAdjustment(int curPos, boolean isLoadMore, String mNo1, String mNo2, String mNo3, String mNo4) {
        showOrHideDialog(true);
        int curPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("page", curPage);
        int maxPageSize = 10;
        map.put("limit", maxPageSize);
        map.put("firstCategoryNo", mNo1);
        map.put("secondCategoryNo", mNo2);
        map.put("thirdCategoryNo", mNo3);
        map.put("workType", mNo4);
        Call<BaseEntity<ListMResponse<List<PriceAdjustmentBean>>>> call = null;
        switch (curPos) {
            case 0:
                call = mRepertory.getApi().getGoodsAdjustPriceList(map);
                break;
            case 1:
                call = mRepertory.getApi().getRentAdjustPriceList(map);
                break;
            case 2:
                call = mRepertory.getApi().getLaowuAdjustPriceList(map);
                break;
        }
        if (call == null) {
            showOrHideDialog(false);
            return;
        }
        call.enqueue(new ICallBack<BaseEntity<ListMResponse<List<PriceAdjustmentBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<PriceAdjustmentBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<PriceAdjustmentBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }
        });
    }

    //  商品类型（industryId:一级类目id，areaCode:省份id)
    public void getAllCategory(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getCatList(mRepertory.getAreaCode()).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData(), isShowLeftDialog);
                }
            }
        });
    }

    // 会员租赁
    public void getAllLeaseType(int type, boolean isShowLeftDialog) {
        // 1机械， 2 设备，3车辆，4 房产，5土地，6工厂，7农机
        // "工程机械", "工程设备", "车辆租赁", "房产租赁", "土地租赁", "工厂租赁", "农机租赁"
        showOrHideDialog(true);
        mRepertory.getApi().getRentCategory().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData(), isShowLeftDialog);
                }
            }
        });
    }

    // 工队-工程分类 type(1 一级分类  type=0 三级分类  type =2 二级分类 type=null  六级分类)
    public void getAllTeamType(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getCategoryTrees(mRepertory.getAreaCode(), "0").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData(), isShowLeftDialog);
                }
            }
        });
    }

    // 工队工种名称
    public void getWorkType(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getWorkType().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setChooseItemData(type, data.getData(), isShowLeftDialog);
                }
            }
        });
    }

    public void getUrlSetting(int curPos, PriceAdjustmentBean bean) {
        showOrHideDialog(true);
        Call<BaseEntity<H5ReEntity>> urlSetting = null;
        switch (curPos) {
            case 0:
                // 商品调价
                urlSetting = mRepertory.getApi().getUrlSetting("goodsAdjPrice");
                break;
            case 1:
                // 租赁调价
                urlSetting = mRepertory.getApi().getUrlSetting("leaseDetails");
                break;
            case 2:
                // 劳务调价
                urlSetting = mRepertory.getApi().getUrlSetting("labourAdjPrice");
                break;
        }
        if (urlSetting == null) {
            showOrHideDialog(false);
            return;
        }
        urlSetting.enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = "";
                    switch (curPos) {
                        case 0:
                            // 商品调价
                            url = data.getData().getUrl() + "?no=" + bean.getItemId() + "&quoteId=" + bean.getQuoteId();
                            break;
                        case 1:
                            // 租赁调价
                            url = data.getData().getUrl() + "?no=" + bean.getBusinessNo() + "&quoteId=" + bean.getQuoteId() + "&type=" + bean.getType();
                            break;
                        case 2:
                            // 劳务调价
                            url = data.getData().getUrl() + "?no=" + bean.getBusinessNo() + "&quoteId=" + bean.getQuoteId();
                            break;
                    }
                    if (!TextUtils.isEmpty(url)) {
                        MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url);
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }
}
